﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#pragma once

#include "IUiView.h"
#include "ITemporaryDimensionHandler.h"
#include "TemporaryDimensionDefine.h"
#include "NumberValidator.h"
#include "LineEditValidateHelper.h"

#include <QWidget>
#include <QLineEdit>


namespace gcmp 
{ 
    class ITemporaryDimensionContext; 
    class Coordinate3d;

    class TemporaryDimensionHandleEditor : public QLineEdit
    {
        Q_OBJECT

    public:
        explicit TemporaryDimensionHandleEditor(QWidget* parent);
        virtual ~TemporaryDimensionHandleEditor();

    private:
        bool m_hasNothingToHandle;
    };


    class TemporaryDimensionHandler : public QWidget, public ITemporaryDimensionHandler
    {
        Q_OBJECT

    public:
        explicit TemporaryDimensionHandler(IUiView* pUIView);
        virtual ~TemporaryDimensionHandler(void);

    public:

        virtual bool IsInInputState() const override;  // TODO: 考虑是否能将此接口访问设为protected

        virtual bool IsInputWidgetEditable() const override;

        virtual void HideInputWidget() override;

        virtual bool UpdateConfig(const ITemporaryDimensionContext* pCtx) override;

        virtual bool IsUserCustomInput() const { return m_isUserCustomInput; }

        virtual void SetUserCustomInput(bool userCustom) { m_isUserCustomInput = userCustom; }

        virtual bool GetCoordinate(Coordinate3d &curCoord) const override;
    protected:
        /*!
        * @brief     激活输入状态, 显示输入框
        * @param[in] InputState     输入状态(长度, 角度, 半径, ...)
        * @param[in] sTabShift        输入框的显示是否由Tab键引发; 默认false
        * @param[in] sOverrideValue   是否重写输入框的值; 若为true, 输入框显示inputValue, 若为false, 则显示当前标注值; 默认false
        * @param[in] inputValue       若isOverrideValue为true, 输入框的显示文本将被设置为inputValue; 默认为空
        */
        virtual void ShowInputWidget(InputState InputState, bool isTabShift = false, bool isOverrideValue = false, std::wstring inputValue = L"") = 0;

        // 创建输入框控件
        void CreateEditorWidget();

        // 设置输入框位置
        void SetEditorPosition(const Vector3d& posOnView);

        void ResetEditorPosition();

        // 更新视图指针
        void UpdateUiView();

        virtual void OnEditorReturnPressed() override {}
    private:
        void SetValidRangeOfLength(const Intervald& range);
        void SetValidRangeOfAngle(const Intervald& range);

        void DisableIme();
        void EnableIme();

    protected:
        // 视图id
        int m_uiViewId;

        // 输入框控件指针（所有权由Qt管理）
        TemporaryDimensionHandleEditor* m_pEditor;
        
        bool                            m_isUserCustomInput; //标记是否用户自定义输入(定义输入不检查合法性)
        gcmp::NumberValidator<double>   m_lenValidator;      //长度范围
        gcmp::NumberValidator<double>   m_angValidator;       //角度范围
        gcmp::OwnerPtr<gcmp::LineEditValidateHelper> m_opValidateHelper;


        // 当前输入状态
        InputState m_InputState;

        // 标注数据, 包括标注值和标注文字位置; 调用GenerateXXDimensions()会刷新
        TemporaryDimensionData m_TmpDimData;

        // 标注样式
        TemporaryDimensionStyle m_TmpDimStyle;

    private:
        // 输入法状态缓存
        HIMC m_hIMC;
        

    };

}


